package cn.com.nes.site.controller.fault;

import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.dto.fault.*;
import cn.com.nes.site.entity.vo.fault.EquipmentFaultInfoListVo;
import cn.com.nes.site.entity.vo.fault.FaultVo;
import cn.com.nes.site.entity.vo.fault.UpdateFaultStateVo;
import cn.com.nes.site.service.fault.FaultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/v2/fault")
public class FaultController {

    @Autowired
    FaultService faultService;

    /**
     * 故障信息-查询列表或单条
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "getEquipmentFaultInfoList", method = RequestMethod.POST)
    @ResponseBody
    public PageData getEquipmentFaultInfoList(@RequestBody EquipmentFaultInfoListVo vo)  throws Exception{
        vo.setManufactureId(vo.getUserInfo().getNes_companyId());
        return faultService.getEquipmentFaultInfoList(vo);
    }

    /**
     * 获取故障趋势列表
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "getFaultTrend", method = RequestMethod.POST)
    @ResponseBody
    public PageData getFaultTrend(@RequestBody FaultVo vo)  throws Exception{
        vo.setManufactureId(vo.getUserInfo().getNes_companyId());
        List<GetFaultTrendDto> dtos = faultService.getFaultTrend(vo);
        return PageData.ok(dtos,true);
    }

    /**
     * 获取故障详情
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "getFaultInfoById", method = RequestMethod.POST)
    @ResponseBody
    public PageData getFaultInfoById(@RequestBody UpdateFaultStateVo vo)  throws Exception{
        FaultInfoDto dto = faultService.getFaultInfoById(vo.getFaultId());
        return PageData.ok("fault", dto);
    }

    /**
     * 更新故障状态 -- 未处理/已处理
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "updateFaultState", method = RequestMethod.POST)
    @ResponseBody
    public PageData updateFaultState(@RequestBody UpdateFaultStateVo vo)  throws Exception{
        vo.setUserId(vo.getUserInfo().getId());
        int res = faultService.updateFaultState(vo);
        if(res>0){
            return PageData.ok();
        }else{
            return PageData.error("修改失败");
        }
    }

    /**
     * 获取故障设备型号趋势图
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "getFaultByProduct", method = RequestMethod.POST)
    @ResponseBody
    public PageData getFaultByProduct(@RequestBody FaultVo vo)  throws Exception{
        vo.setManufactureId(vo.getUserInfo().getNes_companyId());
        vo.setUserId(vo.getUserInfo().getId());
        vo.setDepartmentId(vo.getUserInfo().getDepartmentId());
        // todo 怎样取 getDataRange
        //vo.setDataRange(vo.getUserInfo().get.getDataRange());
        List<GetFaultByProduct> dtos = faultService.getFaultByProduct(vo);
        return PageData.ok(dtos,true);
    }

    /**
     * 获取故障分类趋势图
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "getFaultByFaultType", method = RequestMethod.POST)
    @ResponseBody
    public PageData getFaultByFaultType(@RequestBody FaultVo vo)  throws Exception{
        vo.setManufactureId(vo.getUserInfo().getNes_companyId());
        vo.setUserId(vo.getUserInfo().getId());
        vo.setDepartmentId(vo.getUserInfo().getDepartmentId());
        // todo 怎样取 getDataRange
        //vo.setDataRange(vo.getUserInfo().get.getDataRange());
        List<GetFaultByType> dtos = faultService.getFaultByFaultType(vo);
        return PageData.ok(dtos,true);
    }
}
